package parser

import (
	"fmt"
	"luiz/crawler/engine"
	"luiz/crawler/model"
	"regexp"
	"strings"
)

//访问合租url，获取属性
const (
	rightBox = `<div class="Z_home_b clearfix">[\s\S]+?</div>`
	maps     = `<dd>([^<]+?)</dd>[\s\S]+?<dt>([^<]+?)</dt>`
	idrep    = `[0-9]+`
)

func PreaseDeatil(contents []byte, name string, url string) engine.ParseResult {
	//todo parse id
	compile := regexp.MustCompile(idrep)
	id := compile.FindString(url)
	mapcompile := regexp.MustCompile(maps)
	house := model.House{}
	compile = regexp.MustCompile(rightBox)
	eles := compile.FindString(string(contents))
	result := engine.ParseResult{}
	compile = regexp.MustCompile(maps)
	ParseCp(mapcompile, &house, eles)
	house.Name = name
	item := engine.Item{Url: url, Type: "ziroo", Id: id, Ele: house}
	fmt.Printf("%v", item)
	result.Items = append(result.Items, item)
	return result
}

//todo:两个代码逻辑（map,string 合成一个方法）
func ParseCp(regexp *regexp.Regexp, house *model.House, ele string) {
	submatch := regexp.FindAllStringSubmatch(ele, -1)
	for _, ele := range submatch {

		switch ele[2] {
		case "使用面积":
			split := strings.Split(ele[1], "㎡")
			//float, err := strconv.ParseFloat(split[0], 32)
			//if err != nil {
			//	//todo:找一款专业的日志包，记录日志
			//	log.Printf("string to float32 error:%v,str is %v", err, float)
			//	continue
			//}
			house.MianJ = split[0]
		case "朝向":
			house.ChaoX = ele[1]
		case "户型":
			house.Huxing = ele[1]
		}
	}
}
